package game.scene.map;
import java.util.Vector;
import dgo.misc.TextureManager;
import dgo.gadgets.BaloonWindow;
import dgo.tilemap.MapCharacter;
import javax.microedition.lcdui.Image;
import dgo.Callback;
import dgo.interval.Act;
import dgo.interval.IntervalRunner;
import game.Config;
import game.GlobalModel;
import game.scene.map.WindowFactor;
import starruby.Texture;
import java.util.Hashtable;
import game.scene.map.event.EventCommands;

public class WindowManager {
  private Vector _list;
  private TextureManager _textureManager;
  private GlobalModel _globalModel;
  public WindowManager(GlobalModel globalModel){
    _globalModel = globalModel;
    _list = new Vector();
    _textureManager = new TextureManager();
  }
  
  public BaloonWindow createWindow(String text, String messageType){
    Texture tex = _globalModel.getTextTexture(text);
    BaloonWindow window = new BaloonWindow(0, 0, tex.getWidth() + 12, tex.getHeight() + 12, _textureManager.getTexture("system/" + messageType + "_frame"), 6);;

    Texture base = window.getFrameTexture();
    base.renderTexture(tex, 6, 6);
    return window;
  }
  
  public void push(BaloonWindow window, MapCharacter target, Callback func, EventCommands eventCommands, int autoClose){
    _list.addElement(new WindowFactor(window, target, func, new IntervalRunner(new Act(1, null)), false, eventCommands, autoClose));
  }
  
  public void update(int baseX, int baseY){
    try{
    for(int i = 0; i < _list.size(); i++){
      WindowFactor t = (WindowFactor)(_list.elementAt(i));
      
      if(!t.runner.isDone()){t.runner.update(); }
      if(!t.finished){
        t.window.update((t.target.screenX - baseX + 9) * Config.GAME_SCREEN_SCALE, (t.target.screenY - baseY) * Config.GAME_SCREEN_SCALE, Config.GAME_SCREEN_WIDTH * Config.GAME_SCREEN_SCALE, Config.GAME_SCREEN_HEIGHT * Config.GAME_SCREEN_SCALE);
        if(t.runner.isDone()){ t.func.call(0, t); }
      }else{
        _list.removeElement(t);
      }
    }
    }catch(Exception e){
      System.out.println("WM Error: " + e);
    }
  }
  
  public void render(Texture s, int dx, int dy){
    for(int i = 0; i < _list.size(); i++){
      WindowFactor t = (WindowFactor)(_list.elementAt(i));
      t.window.render(s, dx, dy);
    }
  }
}
